package com.moyq5.permit.center.web.controller;

import com.moyq5.permit.center.web.form.AgentAdminForm4Edit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.PageData;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult.Status;
import com.moyq5.permit.center.bean.embed.AgentAdminId;
import com.moyq5.permit.center.bean.entity.Agent;
import com.moyq5.permit.center.bean.entity.AgentAdmin;
import com.moyq5.permit.center.bean.search.AgentAdmin4Search;
import com.moyq5.permit.center.feign.client.AgentAdminFeign;
import com.moyq5.permit.center.web.jackson.filter.AgentAdminFilter;

/**
 * 代理商管理员
 * @author Moyq5
 * @date 2019年7月25日
 */
@RestController
@RequestMapping("/agentAdmin")
public class AgentAdminController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(AgentAdminController.class);
	
	@Autowired
	private AgentAdminFeign agentAdminFeign;
	
	/**
	 * @author Moyq5
	 * @date 2019年7月30日
	 * @param agentAdmin4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String page(AgentAdmin4Search agentAdmin4s, PageData pd) throws JsonProcessingException {
		log.debug("查询代理商管理员信息>>>page(AgentAdmin4Search, PageData)");
		if (null != getLoginAgentId()) {
			if (null == agentAdmin4s.getAgent()) {
				agentAdmin4s.setAgent(new Agent());
			}
			agentAdmin4s.getAgent().setAgentId(getLoginAgentId());;
		}
		return toJson(agentAdminFeign.page(new Body<AgentAdmin4Search, PageData>(agentAdmin4s, pd)), AgentAdminFilter.get());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param agentAdmin4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/admin", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithAdmin(AgentAdmin4Search agentAdmin4s, PageData pd) throws JsonProcessingException {
		log.debug("查询代理商管理员信息>>>pageWithAdmin(AgentAdmin4Search, PageData)");
		if (null != getLoginAgentId()) {
			if (null == agentAdmin4s.getAgent()) {
				agentAdmin4s.setAgent(new Agent());
			}
			agentAdmin4s.getAgent().setAgentId(getLoginAgentId());;
		}
		return toJson(agentAdminFeign.pageWithAdmin(new Body<AgentAdmin4Search, PageData>(agentAdmin4s, pd)), AgentAdminFilter.getWithAdmin());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param agentAdmin4s
	 * @param pd
	 * @return
	 * @throws JsonProcessingException
	 */
	@GetMapping(value="/page/agentAndAdmin", produces={MediaType.APPLICATION_JSON_UTF8_VALUE})
	@ResponseBody
	public String pageWithAgentAndAdmin(AgentAdmin4Search agentAdmin4s, PageData pd) throws JsonProcessingException {
		log.debug("查询代理商管理员信息>>>pageWithAgentAndAdmin(AgentAdmin4Search, PageData)");
		if (null != getLoginAgentId()) {
			if (null == agentAdmin4s.getAgent()) {
				agentAdmin4s.setAgent(new Agent());
			}
			agentAdmin4s.getAgent().setAgentId(getLoginAgentId());;
		}
		return toJson(agentAdminFeign.pageWithAgentAndAdmin(new Body<AgentAdmin4Search, PageData>(agentAdmin4s, pd)), AgentAdminFilter.getWithAgentAndAdmin());
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param agentAdmin
	 * @return
	 */
	@PutMapping("/add")
	@ResponseBody
	public ObjectResult<AgentAdminId> add(@RequestBody AgentAdmin agentAdmin) {
		log.debug("添加代理商管理员信息>>>add(AgentAdmin)");
		if (null != getLoginAgentId()) {
			agentAdmin.getId().setAgentId(getLoginAgentId());
		}
		ObjectResult<AgentAdmin> objRst = agentAdminFeign.one(agentAdmin.getId());
		if (objRst.getStatus() == Status.OK && null != objRst.getData()) {
			ObjectResult<AgentAdminId> idRst = new ObjectResult<AgentAdminId>();
			idRst.setOk();
			idRst.setData(agentAdmin.getId());
			return idRst;
		}
		return agentAdminFeign.create(agentAdmin);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param agentAdmin
	 * @return
	 */
	@PatchMapping("/edit")
	@ResponseBody
	public StatusResult edit(@RequestBody @Validated AgentAdminForm4Edit agentAdmin) {
		log.debug("更新代理商管理员信息>>>edit(AgentAdmin)");
		if (null != getLoginAgentId()) {
			agentAdmin.getId().setAgentId(getLoginAgentId());
		}
		return agentAdminFeign.update(agentAdmin);
	}
	
	/**
	 * @author Moyq5
	 * @date 2019年7月25日
	 * @param agentAdminId
	 * @return
	 */
	@DeleteMapping("/del")
	@ResponseBody
	public StatusResult del(@RequestBody AgentAdminId agentAdminId) {
		log.debug("删除代理商管理员信息>>>del(AgentAdminId)");
		if (null != getLoginAgentId()) {
			agentAdminId.setAgentId(getLoginAgentId());
		}
		return agentAdminFeign.remove(agentAdminId);
	}
	
}
